package com.lizhenyao.waimai.controller;

import com.lizhenyao.waimai.constant.RedisConstant;
import com.lizhenyao.waimai.dto.OrderDTO;
import com.lizhenyao.waimai.enums.ResultEnum;
import com.lizhenyao.waimai.exception.SellException;
import com.lizhenyao.waimai.service.IOrderService;
import com.lizhenyao.waimai.service.IPayService;
import com.lizhenyao.waimai.utils.JsonUtil;
import com.lly835.bestpay.model.PayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.net.URLDecoder;
import java.util.Map;

/**
 * @Author: lizhenyao
 * @Desciption:
 */
@Controller
@Slf4j
//@RequestMapping("/pay")
public class PayController {

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private IPayService iPayService;

    @GetMapping("/pay")
    public ModelAndView pay(@RequestParam("openid") String openid,
                            @RequestParam("orderId")String orderId,
                            @RequestParam("returnUrl")String returnUrl,
                            Map<String, Object> map){
        log.info("[微信支付] openid={}", openid);

        //1.查询订单
        OrderDTO orderDTO = iOrderService.findOne(orderId);
        if (orderDTO == null){
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }

        //2.发起支付
        PayResponse payResponse = iPayService.create(orderDTO);
        log.info("[预订单创建] payResponse={}", JsonUtil.toJson(payResponse));
        log.info("[预订单创建] returnUrl={}", returnUrl);
        map.put("payResponse", payResponse);
        map.put("returnUrl", URLDecoder.decode(returnUrl));

        return new ModelAndView("pay/create", map);
    }

//    /**
//     * 微信支付预下单
//     * @param orderId
//     * @param returnUrl
//     * @param map
//     * @return
//     */
//    @GetMapping("/create")
//    public ModelAndView create(@RequestParam("orderId")String orderId,
//                               @RequestParam("returnUrl")String returnUrl,
//                               Map<String, Object> map){
//        //1.查询订单
//        OrderDTO orderDTO = iOrderService.findOne(orderId);
//        if (orderDTO == null){
//            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
//        }
//
//        //todo 做一个openid的映射
//        String openid = orderDTO.getBuyerOpenid();
//        String newOpenid = redisTemplate.opsForValue().get(String.format(RedisConstant.OPENID_PREFIX, openid));
//        log.info("[预订单创建] openid={}, newOpenid={}", openid, newOpenid);
//
//        //2.发起支付
//        orderDTO.setBuyerOpenid(newOpenid);
//        PayResponse payResponse = iPayService.create(orderDTO);
//        log.info("[预订单创建] payResponse={}", JsonUtil.toJson(payResponse));
//        log.info("[预订单创建] returnUrl={}", returnUrl);
//        map.put("payResponse", payResponse);
//        map.put("returnUrl", returnUrl);
//
//        return new ModelAndView("pay/create", map);
//    }

    /**
     * 微信支付异步通知
     * @param notify
     * @return
     */
    @PostMapping("/notify")
    public ModelAndView notify(@RequestBody String notify){
        iPayService.notify(notify);

        //返回给微信处理结果
        return new ModelAndView("pay/success");
    }
}
